import java.util.List;

class Solution {
    public boolean[] fg;
    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        int n=rooms.size();
        fg=new boolean[n];
        fg[0]=true;
        dfs(rooms,0);
        for (int i = 0; i < n; i++) {
            if(!fg[i]){
                return false;
            }
        }
        return true;
    }

    private void dfs(List<List<Integer>> rooms, int x) {
        int n=rooms.get(x).size();
        for(int i=0 ;i<n;i++){
            int t=rooms.get(x).get(i);
            if(!fg[t]){
                fg[t]=true;
                dfs(rooms,t);
            }
        }
    }
}